package com.ovopark.organize.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ovopark.organize.server.mapper.FranchiseesApprovalTemplateUserMapper;
import com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateUser;
import com.ovopark.organize.server.service.FranchiseesApprovalTemplateService;
import com.ovopark.organize.server.service.FranchiseesApprovalTemplateUserService;
import com.ovopark.organize.server.util.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wxb
 * @Title: FranchiseesApprovelServiceImpl
 * @Description: 加盟商立项模板字段选择项数据
 * @date 2024/4/11 15:51
 **/
@Service
@Slf4j
public class FranchiseesApprovalTemplateUserServiceImpl extends ServiceImpl<FranchiseesApprovalTemplateUserMapper, FranchiseesApprovalTemplateUser> implements FranchiseesApprovalTemplateUserService {

    @Resource
    private FranchiseesApprovalTemplateService franchiseesApprovalTemplateService;

    /** 
     * @description: 保存或更新
     * @param: [fieldUser]
     * @return: com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateUser
     * @author wxb
     * @date: 2024/6/4 20:02
     */ 
    @Override
    public FranchiseesApprovalTemplateUser saveOrUpdateById(FranchiseesApprovalTemplateUser templateUser) {
        if (templateUser.getId() != null) {
            baseMapper.updateById(templateUser);
        } else {
            baseMapper.insert(templateUser);
        }
        return templateUser;
    }

    /**
     * @description: 获取字段值
     * @param: [groupId, templateId, approvalId, version]
     * @return: com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateFieldUser
     * @author wxb
     * @date: 2024/6/3 15:42
     */
    @Override
    public FranchiseesApprovalTemplateUser getTemplateUser(Integer groupId, Integer templateId, Integer addressId, String version) {
        if (groupId != null && templateId != null && addressId != null) {
            LambdaQueryWrapper<FranchiseesApprovalTemplateUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FranchiseesApprovalTemplateUser::getGroupId, groupId);
            queryWrapper.eq(FranchiseesApprovalTemplateUser::getTemplateId, templateId);
            queryWrapper.eq(FranchiseesApprovalTemplateUser::getAddressId, addressId);
            if (StringUtils.isNotEmpty(version)) {
                queryWrapper.eq(FranchiseesApprovalTemplateUser::getVersion, version);
                queryWrapper.orderByDesc(FranchiseesApprovalTemplateUser::getVersion);
            } else {
                List<String> versionList = franchiseesApprovalTemplateService.getVersionListByTemplateId(templateId, null, groupId);
                if (CollectionUtil.isNotEmpty(versionList)) {
                    queryWrapper.eq(FranchiseesApprovalTemplateUser::getVersion, versionList.get(0));
                }
            }
            queryWrapper.last("limit 1");
            return baseMapper.selectOne(queryWrapper);
        }
        return null;
    }

    /**
     * @description: 获取
     * @param: [templateUserId]
     * @return: com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateUser
     * @author wxb
     * @date: 2024/6/5 10:01
     */
    @Override
    public FranchiseesApprovalTemplateUser getById(Integer templateUserId) {
        if (templateUserId != null) {
            return baseMapper.selectById(templateUserId);
        }
        return null;
    }

    /** 
     * @description: 删除
     * @param: [id]
     * @return: void
     * @author wxb
     * @date: 2024/6/4 20:02
     */ 
    @Override
    public void deleteById(Integer id) {
        if (id != null) {
            baseMapper.deleteById(id);
        }
    }

    /**
     * @description: 地址下的评估记录 分页
     * @param: [page, groupId, addressId]
     * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.ovopark.organize.server.model.entity.FranchiseesApprovalRecords>
     * @author wxb
     * @date: 2024/6/4 19:29
     */
    @Override
    public IPage<FranchiseesApprovalTemplateUser> page(Page<FranchiseesApprovalTemplateUser> page, Integer groupId, Integer addressId) {
        return this.baseMapper.page(page, groupId, addressId);
    }
}
